﻿using System;
using System.Globalization;
using System.Linq;

namespace Medianamik.Core
{
    public class Domain : SystemNode<Domain>
    {
        public override Guid SystemNodeTypeID
        {
            get { return MedianamikTypes.MedianamikDomain; }
        }

        public override CultureInfo DefaultCulture
        {
            get
            {
                return CultureInfo.InvariantCulture;
            }
            set
            {
                ;
            }
        }

        public Guid WebsiteId
        {
            get
            {
                return DefaultInstance.Properties[MedianamikProperties.Domain_Website].GetValue<Guid>();
            }
        }

        public string DomainGroup
        {
            get
            {
                //On fait la vérification pour ne pas faire exploser absolunet.com qui n'a pas (encore) cette Property
                if (NodeType.PropertiesInternal.Any(p => p.ID == MedianamikProperties.Domain_Group))
                {
                    return DefaultInstance.Properties[MedianamikProperties.Domain_Group].GetValue<string>();
                }
                return string.Empty;
            }
        }

        public string Culture
        {
            get
            {
                if (NodeType.PropertiesInternal.Any(p => p.ID == MedianamikProperties.DomainCulture))
                {
                    return DefaultInstance.Properties[MedianamikProperties.DomainCulture].GetValue<string>();
                }

                return string.Empty;
            }
        }

        private WebSite _website;
        public WebSite DomainWebSite
        {
            get
            {
                if (_website == null)
                {
                    _website = NodeManager.GetNode<WebSite>(WebsiteId);
                    _website.DefaultCulture = DefaultCulture;
                }

                return _website;
            }
        }

        public string WebsiteName
        {
            get
            {
                return DomainWebSite.Name;
            }
        }
    }
}